var app = getApp();
var plugin = requirePlugin("myPlugin");

Page({
  data: {
    index: 0,
    weightIndex: 1,
    weightArray: []
  },
  onLoad: function(options) {
    var that = this;
    var user = app.userInfo;

    wx.hideShareMenu()
    plugin.initTraining({
      success: function(res) {
        that.setData(res);
        if (res.club_name) {

          app.middleware.requestAjax({
            serAddr: app.serAddrs.changeClubByGymAddr,
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              gymAddr: res.address,
            },
            cancelToast: true,
            success: function(r) {
              console.log('扫码切换健身房的接口结果', r);
              app.userInfo.gymName = res.club_name;
              app.userInfo.gymAddr = res.address;              
            },
            fail:function(r){
              setTimeout(function () {
                that.setData({
                  msg: r.msg
                })
              }, 1000);
              setTimeout(function () {
                wx.navigateBack()
              }, 3000);
            }
          })
        }
      }
    });

    that.setData({
      user: user,
    })

    if (wx.setKeepScreenOn) wx.setKeepScreenOn({
      keepScreenOn: true
    });
  },

  navigationBack: function() {
    var that = this;
    
    var dialogValue = {
      showDialog: true,
      title: '退出运动',
      content: '训练尚未结束，确定要退出吗？',
      confirmText: '取消',
      cancelText: '退出',
      confirm: function (res) {

      },
      cancel: function (res) {
        wx.switchTab({
          url: app.xhx_route.fitScan,
        })
      }
    }
    this.setData({
      dialogValue: dialogValue
    })
  },

  bindWeightChange: function(e) {
    var that = this;
    var index = e.detail.value;
    that.setData({
      weightIndex: index
    })

    plugin.switchWeight(index);
  },

  onUnload: function() {
    var that = this;

    plugin.detached();
    var jumpInfo = that.data.jumpInfo || {};
    if (jumpInfo.appid) {
      wx.navigateToMiniProgram({
        appId: jumpInfo.appid,
        path: jumpInfo.jumpUrl,
        extraData: jumpInfo.paramResult,
        envVersion: 'release',
      })
    }
  },
  onShareAppMessage: function() {
    return {
      path: app.xhx_route.login
    }
  },
  /**
   * 切换动作
   */
  actionChange: function(event) {
    const that = this;
    var index = event.detail.value;
    that.setData({
      index: index
    })

    plugin.switchAction(index);
  },

  toTrainingActionDetail: function(e) {
    var that = this;
    wx.navigateTo({
      url: app.xhx_route.fitDescribe+'?trainingActionId=' + e.currentTarget.dataset.id,
    })
  }

});